@extends('layouts.app')
@section('title', $product->title)

@section('content')
<div class="row">
<div class="col-lg-ofset-1 col-lg-10">
<div class="panel panel-default">
    <div class="panel-body product-info">
        <div class="row">
            <div class="col-sm-5">
                <img class="cover" src="{{ $product->image_url }}" alt="">
            </div>
            <div class="col-sm-7">
                <div class="title">{{ $product->title }}</div>
                <div class="price"><label>价格</label><em>￥</em><span>{{ $product->price }}</span></div>
                <div class="sales_and_reviews">
                    <div class="sold_count">累计销量 <span class="count">{{ $product->sold_count }}</span></div>
                    <div class="review_count">累计评价 <span class="count">{{ $product->review_count }}</span></div>
                    <div class="rating" title="评分 {{ $product->rating }}">评分 <span class="count">{{ str_repeat('★', floor($product->rating)) }}{{ str_repeat('☆', 5 - floor($product->rating)) }}</span></div>
                </div>
                <div class="skus">
                    <label>选择</label>
                    <div class="btn-group" data-toggle="buttons">
                        @foreach($product->skus as $sku)
                            <label data-price="{{ $sku->price }}"
                                   data-stock="{{ $sku->stock }}"
                                   data-toggle="tooltip"
                                   data-placement="bottom"
                                   class="btn btn-default sku-btn" title="{{ $sku->description }}">
                                <input type="radio" name="skus" autocomplete="off" value="{{ $sku->id }}"> {{ $sku->title }}
                            </label>
                        @endforeach
                    </div>
                </div>
                <div class="cart_amount">
                    <label>数量</label>
                    <input type="text" class="form-control input-sm" value="1">
                    <span>件</span>
                    <span class="stock"></span>
                </div>
                <div class="buttons">
                    @if($favored)
                        <button class="btn btn-success btn-disfover">取消收藏</button>
                    @else
                        <button class="btn btn-success btn-fover">❤ 收藏</button>
                    @endif

                    <button class="btn btn-primary btn-add-to-cart">加入购物车</button>
                </div>
            </div>
        </div>

        <div class="product-detail">
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active">
                    <a href="#product-detail-tab" aria-controls="product-detail-tab" role="tab" data-toggle="tab">商品详情</a>
                </li>
                <li role="presentation">
                    <a href="#product-reviews-tab" aria-controls="product-reviews-tab" role="tab" data-toggle="tab">用户评价</a>
                </li>
            </ul>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="product-detail-tab">
                    {!! $product->description !!}
                </div>
                <div role="tabpanel" class="tab-pane" id="product-reviews-tab">
                    <!-- 评论列表 -->
                    <table class="table table-bordered table-striped">
                        <thead>
                        <tr>
                            <td>用户</td>
                            <td>商品</td>
                            <td>评分</td>
                            <td>评价</td>
                            <td>时间</td>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($reviews as $item)
                        <tr>
                            <td>{{ $item->order->user->name }}</td>
                            <td>{{ $item->productSku->title }}</td>
                            <td>{{ str_repeat('★', $item->rating) }}{{ str_repeat('☆', 5 - $item->rating) }}</td>
                            <td>{{ $item->review }}</td>
                            <td>{{ $item->reviewed_at->format('Y-m-d H:i') }}</td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</div>
@endsection

@section('script')
<script>
$(document).ready(function () {
    $('[data-toggle="tooltip"]').tooltip({trigger: 'hover'});

    $(".sku-btn").on('click', function(){
        $('.product-info .price span').text($(this).data('price'));
        $('.product-info .stock').text('库存：' + $(this).data('stock') + '件');
    });

    // 收藏
    $(".btn-fover").on('click', function () {
        axios.post("{{ route('products.favor', $product->id) }}").then(function () {
            swal('操作成功', '', 'success').then(function () {
                location.reload();
            });
        }, function (error) {
            if (error.response){
                if (error.response.status == 401){
                    return swal('请先登录', '', 'error');
                }

                if (error.response.data.msg){
                    return swal(error.response.data.msg, '', 'error');
                }
            }

            swal('系统错误', '', 'error');
        });
    });

    // 收藏
    $(".btn-disfover").on('click', function () {
        axios.delete("{{ route('products.favor', $product->id) }}").then(function () {
            swal('操作成功', '', 'success').then(function () {
                location.reload();
            });
        }, function (error) {
            if (error.response){
                if (error.response.status == 401){
                    return swal('请先登录', '', 'error');
                }

                if (error.response.data.msg){
                    return swal(error.response.data.msg, '', 'error');
                }
            }

            swal('系统错误', '', 'error');
        });
    });
    
    // 添加购物车
    $(".btn-add-to-cart").on('click', function () {
        var sku_id = $("label.active input[name=skus]").val();
        var amount = $(".cart_amount input").val();
        if (sku_id === undefined){
            // return swal('请选择', '', 'error');
        }
        if (amount == ''){
            return swal('请输入数量', '', 'error');
        }
        var patt = new RegExp(/^[1-9]\d*$/);
        if (!patt.test(amount)){
            return swal('数量输入有误', '', 'error');
        }

        axios.post("{{ route('cart.add') }}", {sku_id, amount}).then(function(){
            swal('加入购物车成功', '', 'success').then(function () {
                location.href = "{{ route('cart.index') }}";
            });
        }, function (error) {
            if (!error.response){
                return swal('系统错误', '', 'error');
            }

            switch (error.response.status) {
                case 401:
                    return swal('请先登录', '', 'error');
                    break;
                case 422:
                    var html = '<div>';
                    _.each(error.response.data.errors, function (errors) {
                        _.each(errors, function (error) {
                            html += error + '<br>';
                        })
                    })
                    html += '</div>';
                    swal({content: $(html)[0], icon: 'error'});
                    break;
            }
        });
    });
})    
</script>
@endsection